<?php

defined('BASEPATH') or die('forbidden');

class General_Controller Extends Controller {

    function _init() {
        check_level(50, segment(0), segment(1));
    }

    function main() {
        $this->view->display("general/main");
    }

    function checkenv() {
        $this->view->display("general/checkenv");
    }

    function personal() {
        $id = $this->auth->user_id;
        $row = $this->db->one("SELECT * FROM {pre}admin WHERE id='{$id}'");
        if (!is_array($row))
            error(translate('C_INVALID_PARAM_ID', $id));
        if (is_submit()) {
            $rowdata = $_POST['row'];
            if ($rowdata['password']) {
                $rowdata['password'] = md5($rowdata['password']);
            } else {
                unset($rowdata['password']);
            }

            $result = $this->db->update("{pre}admin", $rowdata, array('id' => $id));
            if ($result) {
                $rowdata['id'] = $row['id'];
                insert_log(translate('GENERAL_PERSONAL_EDIT', $row['username']));
            }
            success(translate('C_TIPS_SUCCESS_TEXT'), site_url('general/personal'));
        }
        $data['row'] = $row;
        $this->view->display("general/personal", $data);
    }

    function logs() {
        $dirname = array(
            'application' => translate('APPLICATION'),
            'admin' => translate('SHUIHU2012'),
        );
        $rootdir = dirname(BASEPATH);
        $dirlist = scandir($rootdir);
        foreach ($dirlist as $m => $n) {
            $t = $rootdir . '/' . $n;
            if ($n != '.' && $n != '..' && is_dir($t) && is_file($t . '/' . '.htaccess') && is_file($t . '/' . 'index.php')) {
                $logs[$n] = isset($dirname[$n]) ? $dirname[$n] : $n;
            }
        }
        $this->view->assign("dirname", $dirname);
        $act = segment(2, '');
        $listdir = $this->input->post("listdir");
        $listdir = $listdir ? $listdir : array();
        $date = $this->input->post("seldate");
        $date = $date ? $date : date("Y-m-d");
        if ($act == 'getlog') {
            foreach ($logs as $m => $n) {
                if (!in_array($m, $listdir))
                    continue;
                if ($m == 'application') {
                    $logs_path = "{$rootdir}/application/logs/error-{$date}.php";
                } else {
                    $logs_path = "{$rootdir}/{$m}/application/logs/error-{$date}.php";
                }
                if (file_exists($logs_path)) {
                    $filesize = filesize($logs_path);
                    echo "<br /><h3 style='color:red'>{$n} " . translate('DATE') . "[{$date}] " . translate('SIZE') . "[" . format_filesize($filesize) . "] " . translate('GENERAL_LOGS_READ_LINE', $this->input->post("showlines")) . "</h3>";
                    echo "<ol>";
                    $fp = @fopen($logs_path, "r");
                    $pos = -1;
                    $curline = 1;
                    $nums = $this->input->post("showlines");  //读取的行数
                    $orderby = $this->input->post("orderby");  //排序方式
                    $location = $this->input->post("location");  //读取的文件位置,是前还是后
                    $t = ' ';
                    while ($nums > 0) {
                        if ($location == 'front') {
                            //访问前
                            fseek($fp, $pos, SEEK_SET);
                            $pos++;
                            $t = fgetc($fp);
                            if ($t == '')
                                break;
                            if ($t == "\n") {
                                $t = fgets($fp);
                                if ($t == '')
                                    continue;
                                if ($t != "\n") {
                                    if ($orderby == 'asc') {
                                        $t = str_replace("\t", "&nbsp;&nbsp;", $t);
                                        $r = stripos($t, '/api/?v=') !== false ? substr($t, stripos($t, '/api/?v=')) : '';
                                        echo "<li>", str_replace($r, "<b onclick='javascript:rt(this);'>{$r}</b>", $t), "</li>";
                                        $curline++;
                                    }
                                    $nums--;
                                }
                            }
                        } else {
                            //访问后段
                            fseek($fp, $pos, SEEK_END);
                            $pos--;
                            $t = fgetc($fp);
                            if ($t == '')
                                break;
                            if ($t == "\n") {
                                $t = fgets($fp);
                                if ($t == '')
                                    continue;
                                if ($t != "\n") {
                                    if ($orderby == 'desc') {
                                        $t = str_replace("\t", "&nbsp;&nbsp;", $t);
                                        $r = stripos($t, '/api/?v=') !== false ? substr($t, stripos($t, '/api/?v=')) : '';
                                        echo "<li>", str_replace($r, "<b onclick='javascript:rt(this);'>{$r}</b>", $t), "</li>";
                                        $curline++;
                                    }
                                    $nums--;
                                }
                            }
                        }
                    }
                    $nums = $this->input->post("showlines");  //读取的行数
                    $t = ' ';
                    if ($location == 'front' && $orderby == 'desc') {
                        $pos--;
                        while ($nums > 0) {
                            $pos--;
                            fseek($fp, $pos, SEEK_SET);
                            $t = fgetc($fp);
                            if ($t == '')
                                break;
                            if ($t == "\n") {
                                $t = fgets($fp);
                                if ($t == '')
                                    continue;
                                if ($t != "\n") {
                                    $t = str_replace("\t", "&nbsp;&nbsp;", $t);
                                    $r = stripos($t, '/api/?v=') !== false ? substr($t, stripos($t, '/api/?v=')) : '';
                                    echo "<li>", str_replace($r, "<b onclick='javascript:rt(this);'>{$r}</b>", $t), "</li>";
                                    $curline++;
                                    $nums--;
                                }
                            }
                        }
                    } else if ($location == 'end' && $orderby == 'asc') {
                        //重新定位位置
                        fseek($fp, 0);
                        while ($nums > 0) {
                            $pos++;
                            fseek($fp, $pos, SEEK_END);
                            $t = fgetc($fp);
                            if ($t == '')
                                break;
                            if ($t == "\n") {
                                $t = fgets($fp);
                                if ($t == '')
                                    continue;
                                if ($t != "\n") {
                                    $t = str_replace("\t", "&nbsp;&nbsp;", $t);
                                    $r = stripos($t, '/api/?v=') !== false ? substr($t, stripos($t, '/api/?v=')) : '';
                                    echo "<li>", str_replace($r, "<b onclick='javascript:rt(this);'>{$r}</b>", $t), "</li>";
                                    $curline++;
                                    $nums--;
                                }
                            }
                        }
                    }
                    fclose($fp);
                    echo "</ol>";
                } else {
                    echo "<br /><h3 style='color:green'>{$n} " . translate('DATE') . "[{$date}] " . translate('GENERAL_LOGS_NOT_EXIST') . "</h3>";
                }
            }
        } else if ($act == 'getrequestdata') {
            $url = $this->input->get("url", "stripslashes");
            $url = substr($url, 0, 7) == 'http://' ? $url : dirname(base_url(1)) . $url;
            $ret = request($url, array(), "GET");
            echo $ret['msg'];
        } else if ($act == 'clearlog') {
            foreach ($logs as $m => $n) {
                if (!in_array($m, $listdir))
                    continue;
                if ($m == 'application') {
                    $logs_path = "{$rootdir}/application/logs/error-{$date}.php";
                } else {
                    $logs_path = "{$rootdir}/{$m}/application/logs/error-{$date}.php";
                }
                if (is_file($logs_path))
                    @unlink($logs_path);
            }
            insert_log(translate('GENERAL_LOGS_CLEARLOG', $date));
            echo "<br /><font color=green>" . translate('C_TIPS_SUCCESS_TEXT') . " " . translate('GENERAL_LOGS_CLEARLOG', $date) . "</font>";
        }else {
            $this->view->display("general/logs");
        }
    }

}